local joy__kuangbi = fk.CreateSkill {

  name = "joy__kuangbi",

  tags = {  },

}



joy__kuangbi:addEffect("active", {
  name = "joy__kuangbi",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#joy__kuangbi",
  can_use = function(self, player)
    return player:usedSkillTimes(joy__kuangbi.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return false
  end,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id and not Fk:currentRoom():getPlayerById(to_select):isNude()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = room:askForCard(target, 1, 3, true, joy__kuangbi.name, false, ".", "#joy__kuangbi-card:"..player.id)
    local dummy = Fk:cloneCard("dilu")
    dummy:addSubcards(cards)
    player:addToPile(joy__kuangbi.name, dummy, false, joy__kuangbi.name)
    if player.dead or target.dead then return end
    if room:askForSkillInvoke(player, joy__kuangbi.name, nil, "#joy__kuangbi-draw::"..target.id..":"..#dummy.subcards) then
      target:drawCards(#dummy.subcards, joy__kuangbi.name)
    end
  end,
})

joy__kuangbi:addEffect(fk.TurnStart, {
  name = "#joy__kuangbi_trigger",
  mute = true,
  events = {fk.TurnStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and #player:getPile("joy__kuangbi") > 0
    elseif player:hasSkill("joy__kuangbi") and (table.every(player:getCardIds("h"), function(id)
      return Fk:getCardById(id):getMark("@@joy__kuangbi") == 0 end)) then
      for _, move in ipairs(data) do
        if move.from==player and move.extra_data and move.extra_data.joy__kuangbi then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("joy__kuangbi")
    room:notifySkillInvoked(player, "joy__kuangbi", "support")
    if event == fk.TurnStart then
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(player:getPile("joy__kuangbi"))
      for _, id in ipairs(player:getPile("joy__kuangbi")) do
        room:setCardMark(Fk:getCardById(id), "@@joy__kuangbi", 1)
      end
      room:obtainCard(player, dummy, false, fk.ReasonJustMove)
    else
      player:drawCards(1, "joy__kuangbi")
      if player:isWounded() and not player.dead then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = "joy__kuangbi",
        }
      end
    end
  end,
})
joy__kuangbi:addEffect(fk.AfterCardsMove, {
  name = "#joy__kuangbi_trigger",
  mute = true,
  events = {fk.TurnStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and #player:getPile("joy__kuangbi") > 0
    elseif player:hasSkill("joy__kuangbi") and (table.every(player:getCardIds("h"), function(id)
      return Fk:getCardById(id):getMark("@@joy__kuangbi") == 0 end)) then
      for _, move in ipairs(data) do
        if move.from==player and move.extra_data and move.extra_data.joy__kuangbi then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("joy__kuangbi")
    room:notifySkillInvoked(player, "joy__kuangbi", "support")
    if event == fk.TurnStart then
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(player:getPile("joy__kuangbi"))
      for _, id in ipairs(player:getPile("joy__kuangbi")) do
        room:setCardMark(Fk:getCardById(id), "@@joy__kuangbi", 1)
      end
      room:obtainCard(player, dummy, false, fk.ReasonJustMove)
    else
      player:drawCards(1, "joy__kuangbi")
      if player:isWounded() and not player.dead then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = "joy__kuangbi",
        }
      end
    end
  end,
})
joy__kuangbi:addEffect(fk.BeforeCardsMove, {
  refresh_events = {fk.BeforeCardsMove},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill("joy__kuangbi") then
      for _, move in ipairs(data) do
        if move.from==player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("@@joy__kuangbi") > 0 then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from==player then
        local yes = false
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("@@joy__kuangbi") > 0 then
            player.room:setCardMark(Fk:getCardById(info.cardId), "@@joy__kuangbi", 0)
            yes = true
          end
        end
        if yes then
          move.extra_data = move.extra_data or {}
          move.extra_data.joy__kuangbi = true
        end
      end
    end
  end,
})

return joy__kuangbi